package com.etheart.music.web.action.index;

import java.util.Map;

import javax.management.monitor.Monitor;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.etheart.framework.core.dao.QueryService;
import com.etheart.framework.core.fulltextsearch.IndexService;
import com.etheart.framework.core.fulltextsearch.MassIndexerProgressMonitorImpl;
import com.etheart.model.entity.Album;
import com.etheart.model.entity.Singer;
import com.etheart.model.entity.Song;
import com.etheart.music.web.action.index.helper.IndexThread;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.mail.handlers.message_rfc822;

@Controller("indexAction")
@Scope("prototype")  
@ParentPackage("json-default")
public class IndexAction extends ActionSupport implements ApplicationAware,RequestAware{
	  
	private static final long serialVersionUID = 1L;

	@Autowired                            
	private IndexService indexService;
	
	@Action(value="rebuild",results={@Result(name="success", location="/index/indexManagement.jsp")})  
	public String rebuild()  {
		IndexThread indexThread = getIndexThread();
		if (indexThread!=null&&indexThread.isAlive()) {
			request.put("message", "线程 未完成");
			return "success";
		}
		indexThread.start();
		putIndexThreadToApplication(indexThread);
		return "success";
	}
	
	private void putIndexThreadToApplication(IndexThread indexThread) {
		application.put("music_indexThread",indexThread);
	}

	private MassIndexerProgressMonitorImpl getMonitor() {
		Object m = application.get("music_monitor");
		if (m == null) {
			return null;
		}
		return (MassIndexerProgressMonitorImpl) m;
	}
	
	private IndexThread getIndexThread() {
		Object th = application.get("music_indexThread");
		if (th == null) {
			IndexThread thread = new IndexThread(indexService);
			return thread;
		}
		return (IndexThread) th;
	}

	@Action(value="refresh",results={@Result(name="success", location="/index/indexManagement.jsp")})  
	public String refresh()  {
		IndexThread thread = getIndexThread();
		if (thread == null || !thread.isAlive()) {
			request.put("message", "线程未启动");
		}else{
			request.put("message", thread.getMessage());
		}
		return "success";
	}
	
	
	private Map<String, Object> application ;
	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}
	
	private Map<String, Object> request ;
	@Override
	public void setRequest(Map<String, Object> request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

}
